import threading
import time

from inference_old import GenModule
import json
import cv2 as cv
import numpy as np

from plots import colors


# *********************基本流程请查看ViewAll*********************


# 测试内存占用；单功能示例参考
class ViewAll(GenModule):

    def sub_function(self, return_xyxys, return_names, return_confs):
        a = self.src_num
        for i in range(self.src_num):  # 遍历4路的检测结果
            # 以下均为单帧结果 xyxys左上角右下角坐标 names类别名称 confs置信度
            # xyxys，names，confs为列表，同一个目标在三个列表中的索引一一对应
            xyxys = return_xyxys[i]
            names = return_names[i]
            confs = return_confs[i]

            # ——————————————————————功能实现——————————————————————
            # 在返回给网页的画面(self.img1s[i])中画框，不影响保存视频中的画框

            # 画出所有目标
            for j, xyxy in enumerate(xyxys):
                self.boxed_img(xyxy, self.img1s[i], (255, 0, 0), names[j])

            # ——————————————————————结果返回——————————————————————
            self.results_process(
                False,  # 是否违规：True违规，False不违规
                i,  # 视频流下标
                xyxys  # 保存视频中标注的坐标，列表
            )


class ketang(GenModule):
    def sub_function(self, return_xyxys, return_names, return_confs, return_cls):
        for i in range(len(return_xyxys)):  # 遍历所有摄像头的检测结果
            # 以下均为单帧结果 xyxys左上角右下角坐标 names类别名称 confs置信度
            # xyxys，names，confs为列表，同一个目标在三个列表中的索引一一对应
            xyxys = return_xyxys[i]
            names = return_names[i]  # 模型推理的一帧图像上的动作名称列表   --Pear #
            cls = return_cls[i]
            # ——————————————————————功能实现——————————————————————
            # 在返回给网页的画面(self.img1s[i])中画框，不影响保存视频中的画框
            # if "fight" in names or 'gather' in names:  # luohuanCode
            #     self.safety_class[i] = 1  # self.safety_class是安全等级
            # elif "phone" in names or "sleep" in names or 'hand' in names or 'stand' in names:  # PearCode 睡觉、玩手机、举手、站立四个行为检测到变化就推送
            #     self.safety_class[i] = 2
            # else:
            #     self.safety_class[i] = 3
            # 画出所有目标
            for j, xyxy in enumerate(xyxys):
                self.boxed_img(xyxy, self.img1s[i], colors(cls[j]), names[j])
        # 主线程继续进行图片展示，消息后处理判断交给其他线程
        t = threading.Thread(target=self.resutl_save(return_names))
        t.start()  # self.safety_class是安全等级, i是摄像头的id, names是label-name  --Pear #
        # self.resutl_save(return_names)
        return return_names


class ketang_back(GenModule):
    def sub_function_sort(self, return_xyxys, return_names, return_confs, return_cls, return_ids):
        for i in range(len(return_xyxys)):  # 遍历4路的检测结果
            # 以下均为单帧结果 xyxys左上角右下角坐标 names类别名称 confs置信度
            # xyxys，names，confs为列表，同一个目标在三个列表中的索引一一对应
            xyxys = return_xyxys[i]
            names = return_names[i]
            cls = return_cls[i]
            ids = return_ids[i]

            # ——————————————————————功能实现——————————————————————
            # 在返回给网页的画面(self.img1s[i])中画框，不影响保存视频中的画框

            # 画出所有目标
            for j, xyxy in enumerate(xyxys):
                have_person = False
                if self.k_time[i] < 60 and names[j] == 'stand':
                    self.sort_id[i].append(ids[j])
                    self.k_time[i] = self.k_time[i] + 1
                if self.k_time[i] == 60:
                    self.current_id[i] = np.argmax(np.bincount(self.sort_id[i]))
                if int(ids[j]) == self.current_id[i]:
                    have_person = True
                    self.boxed_img(xyxy, self.img1s[i], colors(cls[j]), names[j])
                    if True:
                        x0 = int((int(xyxy[0]) + int(xyxy[2])) / 2)
                        y0 = int((int(xyxy[1]) + int(xyxy[3])) / 2)
                        # center = (x0, y0)
                        self.TMPPoints[i].append({"x": x0, "y": y0, "t": time.time()})
                        self.points[i].append([x0, y0])
                        # cv.circle(self.img1s[i], center, 1, (255, 0, 0), cv.FILLED)
                        # for point in self.points[i]:
                        #     cv.circle(self.img1s[i], (point[0], point[1]), 1, (255, 255, 255), cv.FILLED)
                if names[j] == 'smoke':
                    self.boxed_img(xyxy, self.img1s[i], colors(cls[j]), names[j])
                if have_person == False and self.k_ztime[i] == 60:
                    self.sort_id[i].clear()
                    self.k_time[i] = 0
            # ——————————————————————结果返回——————————————————————
            # self.results_process(
            #     False,  # 是否违规：True违规，False不违规
            #     i,  # 视频流下标
            #     xyxys  # 保存视频中标注的坐标，列表
            # )
            # if "smoke" in names:
            #     self.safety_class[i] = 2  # self.safety_class是安全等级
        # self.resutl_save(return_names)
        t = threading.Thread(target=self.resutl_save(return_names))
        t.start()

        return return_names
